﻿using System;
using System.ComponentModel;
using System.Drawing;

namespace Pibby.Controls
{
	public abstract class ValidatingTextBox : System.Windows.Forms.TextBox
	{
		public ValidatingTextBox()
			: base ()
		{
			this.ErrorColor = Color.Red;
		}

		//[TestCategory("Appearance")]
		[DefaultValue(typeof(Color), "Red")]
		[Browsable(true)]
		public Color ErrorColor
		{
			get;
			set;
		}

		public string ValidationMessage
		{
			get;
			set;
		}

		protected override void OnTextChanged(EventArgs e)
		{
			// Let the base class process changed text first
			base.OnTextChanged(e);

			if (!this.Validate(this.Text))
			{
				this.ForeColor = this.ErrorColor;
			}
			else
			{
				this.ForeColor = this.ForeColor;
			}
		}

		protected abstract bool Validate(string text);
	}
}
